package gateway.ontology;

import gateway.ontology.classes.GetRemoteAgents;
import gateway.ontology.classes.RegisterAgent;
import gateway.ontology.classes.UnregisterAgent;
import gateway.ontology.classes.basic_translation.Header;
import gateway.ontology.classes.basic_translation.Message;
import gateway.ontology.classes.basic_translation.ReceivedMessage;
import gateway.ontology.classes.basic_translation.SendMessage;
import gateway.ontology.classes.basic_translation.SensorID;
import gateway.ontology.classes.basic_translation.TerminateSensor;
import jade.content.onto.BasicOntology;
import jade.content.onto.Ontology;
import jade.content.onto.OntologyException;
import jade.content.schema.AgentActionSchema;
import jade.content.schema.ConceptSchema;
import jade.content.schema.ObjectSchema;
import jade.content.schema.PredicateSchema;
import jade.content.schema.PrimitiveSchema;

public class GatewayOntology extends Ontology implements
		GatewayOntologyVocabulary {
	public static final String ONTOLOGY_NAME = "JADE-MAPS-gateway-ontology";

	private static Ontology theInstance = new GatewayOntology();

	public static Ontology getInstance() {
		return theInstance;
	}

	private GatewayOntology() {
		super(ONTOLOGY_NAME, BasicOntology.getInstance());

		try {

			/* *************************************************************************************
			 * Basic functions of the gateway which cover : registration,
			 * deregistration and sending a list of available MAPS agents
			 * *************************************************************************************/
			
			// Agent actions
			add(new AgentActionSchema(REGISTER), RegisterAgent.class);
			add(new AgentActionSchema(UNREGISTER), UnregisterAgent.class);
			add(new AgentActionSchema(GET_REMOTE_AGENTS), GetRemoteAgents.class);

			// Structure for the schema for the Unregister agent action
			AgentActionSchema as = (AgentActionSchema) getSchema(UNREGISTER);
			as.add(AGENTAID, (ConceptSchema) getSchema(BasicOntology.AID),
					ObjectSchema.MANDATORY);

			// Structure for the schema for the Register agent action
			as = (AgentActionSchema) getSchema(REGISTER);
			as.add(AGENTAID, (ConceptSchema) getSchema(BasicOntology.AID),
					ObjectSchema.MANDATORY);
			as.add(SUFFIX, (PrimitiveSchema) getSchema(BasicOntology.STRING),
					ObjectSchema.OPTIONAL);
			
			/* *************************************************************************************
			 * Basic functions of the gateway which cover : sending and receiving messages
			 * *************************************************************************************/
			
			// Concepts
			add(new ConceptSchema(SENSOR_ID), SensorID.class);
			add(new ConceptSchema(HEADER), Header.class);
			add(new ConceptSchema(MESSAGE), Message.class);
			// Predicate
			add(new PredicateSchema(RECEIVED_MESSAGE), ReceivedMessage.class);
			// AgentActions
			add(new AgentActionSchema(SEND_MESSAGE), SendMessage.class);
			add(new AgentActionSchema(TERMINATE_SENSOR), TerminateSensor.class);
			
			// Structure for the schema for the event header concept
			ConceptSchema cs = (ConceptSchema) getSchema(HEADER);
			cs.add(SOURCEID, (ConceptSchema) getSchema(BasicOntology.AID), ObjectSchema.MANDATORY);
			cs.add(TARGETID, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY);
			
			// Structure for the schema for the message concept
			cs = (ConceptSchema) getSchema(MESSAGE);
			cs.addSuperSchema((ConceptSchema) getSchema(HEADER));
			cs.add(MESSAGE_NAME,
					(PrimitiveSchema) getSchema(BasicOntology.INTEGER), ObjectSchema.OPTIONAL);
			cs.add(MESSAGE_TYPE,
					(PrimitiveSchema) getSchema(BasicOntology.INTEGER), ObjectSchema.OPTIONAL);
			cs.add(MESSAGE_PARAMETERS,
					(PrimitiveSchema) getSchema(BasicOntology.STRING), 0,
					ObjectSchema.UNLIMITED);
			
			// Structure for the schema for the sensor id concept
			cs = (ConceptSchema) getSchema(SENSOR_ID);
			cs.addSuperSchema((ConceptSchema) getSchema(HEADER));
			cs.add(ID, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY);
			
			// Structure for the schema for the Send back predicate
			PredicateSchema ps = (PredicateSchema) getSchema(RECEIVED_MESSAGE);
			ps.add(MESSAGE_WITH_HEADER, (ConceptSchema) getSchema(HEADER), ObjectSchema.MANDATORY);
			
			// Structure for the schema for the send message agent action
			as = (AgentActionSchema) getSchema(SEND_MESSAGE);
			as.addSuperSchema((ConceptSchema) getSchema(MESSAGE));
			
			// Structure for the schema for the terminate sensor agent action
			as = (AgentActionSchema) getSchema(TERMINATE_SENSOR);
			as.addSuperSchema((ConceptSchema) getSchema(HEADER));
			as.add(ID, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY);

		} catch (OntologyException oe) {
			oe.printStackTrace();
		}
	}

}
